/* Copyright (c) 2011-2013 ISP RAS, Moscow. For more information please write to

      mermakov@ispras.ru
      svartanov@ispras.ru

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package ru.ispras.coffeemachine.util;

import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;

/**
 * Logger
 *
 * @author Mikhail Ermakov (mermakov@ispras.ru)
 * @version 1.2.0
 * @since 2011-09-12
 */
public class Logger
{
	public static class VerboseLevel extends Level {

		/**
		 *
		 */
		private static final long serialVersionUID = -3847182673114131518L;

		public static final int VERBOSE_INT = INFO_INT - 10;

		public static final String VERBOSE_STR = "VERBOSE";

		public static final Level VERBOSE = new VerboseLevel(VERBOSE_INT, VERBOSE_STR, 8);

		protected VerboseLevel(int level, String levelStr, int syslogEquivalent) {

			super(level, levelStr, syslogEquivalent);
		}

		public static Level toLevel(int val) {

			return toLevel(val, WARN);
		}

		public static Level toLevel(String arg) {

			return toLevel(arg, WARN);
		}

		public static Level toLevel(int val, Level defaultLevel) {

			if (val == VERBOSE_INT) {
				return VERBOSE;
			}

			return defaultLevel;
		}

		public static Level toLevel(String arg, Level defaultLevel) {

			if (arg != null) {
				if (arg.toUpperCase().equals(VERBOSE_STR)) {
					return VERBOSE;
				}
			}

			return defaultLevel;
		}

	}

	private static final String DEFAULT_LOG_LAYOUT_PATTERN = "%-4r %-5p %c %x - %m%n";

	private static org.apache.log4j.Logger log;

	static {
		try {
			log = org.apache.log4j.Logger.getLogger(Logger.class);
			if (!log.getAllAppenders().hasMoreElements()) {
				throw new Exception();
			}
		} catch (Exception e) {
			log = org.apache.log4j.Logger.getRootLogger();
			log.setLevel(org.apache.log4j.Level.INFO);
			Appender defaultAppender = new ConsoleAppender();
			((ConsoleAppender)defaultAppender).setWriter(new OutputStreamWriter(System.out));
			defaultAppender.setName("stdout");
			defaultAppender.setLayout(new PatternLayout(DEFAULT_LOG_LAYOUT_PATTERN));
			log.addAppender(defaultAppender);
		}
	}

	public static void info(Object message)
	{
		log.info(message);
	}

	public static void verbose(Object message)
	{
		log.log(VerboseLevel.VERBOSE, message);
	}

	public static void debug(Object message)
	{
		log.debug(message);
	}

	public static void error(Object message)
	{
		log.error(message);
	}

	public static void warn(Object message)
	{
		log.warn(message);
	}

	public static void exploit(String message, int exploitNumber) throws IOException
	{
		System.out.println("\033[31m\nExploit " + exploitNumber + "\033[0m" + "\n" + message + "\nInformation about exploit in exploits/" + exploitNumber + "\n");
	}

}
